package com.alliky.core.net.interceptor;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.alliky.core.util.AesUtil;
import com.alliky.core.util.StringUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.DeviceUtils;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * ================================================
 *
 * @Description: 描述
 * @Author: wxianing
 * @Date: 2023/7/17 11:16
 * ================================================
 */
public class CommonParamsIntercepter implements Interceptor {


    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder newRequestBuilder = request.newBuilder();
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("timestamp", timestamp);
        hashMap.put("sys_type", "Cashier");
        hashMap.put("client_device", "android");
        hashMap.put("version_name", AppUtils.getAppVersionName());
        hashMap.put("device_number", DeviceUtils.getAndroidID());

        if (StringUtils.isNotBlank("token")) {
            hashMap.put("token", "4ef67fa63f5df990401d05d26d822622");
        }
        hashMap.put("sign", AesUtil.setSign2(hashMap));

        hashMap.forEach((k, v) -> {
            newRequestBuilder.addHeader(k, v);
        });

        RequestBody requestBody = request.body();
        if (requestBody instanceof FormBody) {
            FormBody formBody = (FormBody) requestBody;
            FormBody.Builder formBodyBuilder = new FormBody.Builder();
//            String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
//            HashMap<String, String> hashMap = new HashMap();
//            hashMap.put("timestamp", timestamp);
//            hashMap.put("sys_type", "Cashier");
//            hashMap.put("client_device", "android");
//            hashMap.put("version_name", AppUtils.getAppVersionName());
//            hashMap.put("device_number", RxDeviceUtils.getUniqueDeviceId(AppConfig.getApplicationContext()));

//            for (int i = 0; i < formBody.size(); i++) {
//                hashMap.put(formBody.encodedName(i), formBody.encodedValue(i));
//            }
//            if (StringUtil.isNotBlank(Global.SP.getToken())) {
//                hashMap.put("token", Global.SP.getToken());
//            }
//            hashMap.put("sign", MD5Util.getSign2(hashMap));
//            hashMap.forEach((k, v) -> {
//                formBodyBuilder.addEncoded(k, v);
//            });
            newRequestBuilder.post(formBodyBuilder.build());
        } else {
            MultipartBody multipartBody = (MultipartBody) requestBody;
            MultipartBody.Builder newMultipartBodyBuilder = new MultipartBody.Builder();
            //公共参数设置处
//            newMultipartBodyBuilder.addPart(Headers.of("Content-Disposition: form-data; name=\"token\""), RequestBody.create("123456", null));
            //或者
//            newMultipartBodyBuilder.addFormDataPart("token", "123456");
            for (int i = 0; i < multipartBody.size(); i++) {
                MultipartBody.Part part = multipartBody.part(i);
                newMultipartBodyBuilder.addPart(part);
            }
            newRequestBuilder.post(multipartBody);
        }
        return chain.proceed(newRequestBuilder.build());
    }
}
